package com.freshcity.page.welcome
{
	import com.freshcity.component.container.HBox;
	import com.freshcity.component.container.VBox;
	import com.freshcity.core.BasePage;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.manager.LayerManager;
	import com.greensock.TweenLite;
	import com.pblabs.core.PBGroup;
	
	import fl.controls.Button;
	import fl.controls.Label;
	import fl.controls.TextInput;
	
	import flash.events.MouseEvent;

	public class Login extends BasePage
	{
		private var loginId:TextInput;
		private var mainGroup:VBox;

		override public function initialize():void
		{
			super.initialize();

			var title:Label=new Label();
			title.text="登录";
			title.width = 100;
			
			var label:Label=new Label();
			label.text="帐号";
			label.width=30;
			loginId=new TextInput();
			loginId.text = "tom";
			loginId.width=40;

			var box:HBox=new HBox();
			box.alignment=HBox.MIDDLE;
			box.addChild(label);
			box.addChild(loginId);
			box.initialize();

			var submit:Button=new Button();
			submit.width=40;
			submit.addEventListener(MouseEvent.CLICK, onSubmitClick);
			submit.label="登录";
			var initBtn:Button=new Button();
			initBtn.width=40;
			initBtn.addEventListener(MouseEvent.CLICK, onRegisterClick);
			initBtn.label="注册";

			var box2:HBox=new HBox();
			box2.alignment=HBox.MIDDLE;
			box2.addChild(submit);
			box2.addChild(initBtn);
			box2.initialize();
			
			
			mainGroup = new VBox();
			mainGroup.alignment = VBox.CENTER;
			mainGroup.width = 200;
			mainGroup.hasBack=true;
			mainGroup.addChild(title);
			mainGroup.addChild(box);
			mainGroup.addChild(box2);
			mainGroup.initialize();
			
			LayerManager.sceneLayer.centerPopup(mainGroup);
			rootClass.addChild(mainGroup);
		}
		
		override public function destroy():void
		{
			super.destroy();
		}
		
		protected function onRegisterClick(event:MouseEvent):void
		{
			var page:DataInit=new DataInit();
			page.owningGroup=this.owningGroup;
			page.loginUser={};
			page.initialize();
			
			TweenLite.to(this.rootClass,0.3,{x:"-300",alpha:0});
			//TweenLite.from(page.rootClass,0.4,{onComplete:destroy,x:"+600",delay:0.4});
		}
		
		protected function onSubmitClick(event:MouseEvent):void
		{
			var userId:String=loginId.text;
			commu.send(OpCodes.LOGIN, {id:userId,pass:""}, onLogin);
		}

		private function onLogin(data:Object):void
		{
			var main:MainScene = new MainScene();
			main.user = data.user;
			main.cond = data.cond;
			GlobalVar.LOGIN_USER = main.user;
			main.owningGroup = this.owningGroup;
			main.initialize();
			this.destroy();
		}
	}
}
